/*
 * CS_EVOLUTION_ITE_CREATE.cpp
 *
 *  Created on: 2013. 6. 17.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/VirtualCurrency.h"
#include "../../NResource/Static.h"

#include "../../GameLog.h"

namespace ProjectZ { namespace NCommand {

void SendFailEvolutionItemCreate(User * pUser, int res)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << static_cast<int16_t>(res);
	*pAck << static_cast<uint8_t>(0);
	*pAck << static_cast<uint64_t>(0);
	*pAck << static_cast<uint8_t>(0);
	*pAck << static_cast<uint8_t>(0);
	*pAck << static_cast<uint16_t>(0);
	*pAck << static_cast<uint16_t>(0);
	*pAck << static_cast<uint16_t>(0);
	pAck->MakePacket(CMD_SC_EVOLUTION_ITEM_CREATE);
	pUser->Send(pAck);
}

void Command::CS_EVOLUTION_ITEM_CREATE(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * /// 진화아이템 생성
	CS_EVOLUTION_ITEM_CREATE                                                            0x1804
	{
			   u8                  u8bag_type;                                                                      /// 가방 타입
			   u64                 u64item_seq;                                                           /// 가방에서 몇번째 슬롯
	}

	SC_EVOLUTION_ITEM_CREATE                                                            0x1805
	{
			   s16                 s16Ack;                                                                            /// 포인트 부족 등 ack
			   u8                  u8bag_type;                                                                      /// 가방 타입
			   u64                 u64item_seq;                                                           /// 가방에서 몇번째 슬롯

			   /// 변경된 아이템 진화 값들
			   u8  evolve_max;                                                                            /// 최대 진화 단계
			   u8        evolve_step;                                                                      /// 현재 진화단계
			   u16      evolve_point;                                                                     /// 진화포인트
			   u16      evolve_eff;                                                                                   /// 진화당 증가하는 증가분(%)
			   u16      evolve_eff_value;                                                       /// 진화이펙트 값(누적)
	}
	 *
	 */
	PREPARE_PACKET

	if (false == pUser->EndBattle_afterEffect())
	{
		SendFailEvolutionItemCreate(pUser, ACK_BATTLE_AFTEREFFECT);
		return;
	}

	char u8bag_type	= 0;
	uint64_t u64item_seq	= 0;

	*pPacket >> u8bag_type >> u64item_seq;

	NResource::VCResource::Flyweight * pFly =
			NResource::Static::InstancePtr()->GetVCRef()->GetFly(EM_VCINDEX_CREATE_EVOLUTION_ITEM);

	if (NULL == pFly)
	{
		PROCESSLOG("CS_EVOLUTION_ITEM_CREATE : pFly is NULL! vcindex=%d", EM_VCINDEX_CREATE_EVOLUTION_ITEM);
		return;
	}

	// 보유젠 확인
	if (pUser->GetVirtualCurrency()->GetZen() < (pFly->_VC_PRICE * -1) )
	{
		SendFailEvolutionItemCreate(pUser, ACK_NOT_ENOUGH_ZEN);
		return;
	}

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(u8bag_type));

	if (NULL == pBag)
	{
		PROCESSLOG("CS_EVOLUTION_ITEM_CREATE : pBag is NULL!! bagType=%d", u8bag_type);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(u64item_seq);

	if (NULL == pItem)
	{
		PROCESSLOG("CS_EVOLUTION_ITEM_CREATE : pItem is NULL!! u64item_seq=%d", u64item_seq);
		return;
	}

	// 이미 진화 아이템인 경우
	if (pItem->GetData().evolve_max > 0)
	{
		SendFailEvolutionItemCreate(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 미식별 아이템은 진화아이템으로 만들 수 없다
	if (pItem->GetData().non_identity != 0)
	{
		SendFailEvolutionItemCreate(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	pItem->GetData().evolve_step	= 0;
	pItem->GetData().evolve_point	= 0;
	pItem->GetData().evolve_value	= 0;
	pItem->GetData().evolve_percent	= NResource::Static::Instance().GetItemEvolution()->GetEvolveEffect(pUser);
	pItem->GetData().evolve_max		= NResource::Static::Instance().GetItemEvolution()->GetEvolveMaxStep(pUser, pItem->GetData().level);

	if (false == pItem->UpdateDatabaseItemInfo())
	{
		PROCESSLOG("CS_EVOLUTION_ITEM_CREATE : Item Update Fail!!");
		SendFailEvolutionItemCreate(pUser, ACK_DB_ERROR);
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << u8bag_type;
	*pAck << static_cast<uint64_t>(u64item_seq);
	*pAck << static_cast<uint8_t>(pItem->GetData().evolve_max);
	*pAck << static_cast<uint8_t>(pItem->GetData().evolve_step);
	*pAck << static_cast<uint16_t>(pItem->GetData().evolve_point);
	*pAck << static_cast<uint16_t>(pItem->GetData().evolve_percent);
	*pAck << static_cast<uint16_t>(pItem->GetData().evolve_value);
	pAck->MakePacket(CMD_SC_EVOLUTION_ITEM_CREATE);
	pUser->Send(pAck);

	// VC 처리
	NLogic::NFunction::VCProcess(pUser, EM_VCINDEX_CREATE_EVOLUTION_ITEM);

	NGameLog::LogItemInfo(pUser, CREATE_EVOLUTION_ITEM, 1, 0, pItem);

}

} /*NCommand*/ } /*ProjectZ*/

